<?php

namespace Api\Controller;

use Common\Controller\ApiController;

/**
 * 用户API接口
 */
class NewsController extends ApiController {

    /**
     * 获取新闻列表
     * @author Pirlo.21 <375057009@qq.com>
     * @dateTime 2016-12-27
     */
    public function newsList() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'add_time');
        $orderDirection = I('post.orderDirection', 'DESC');
        $map['status'] = 1;
        $total = M('News')->where($map)->count();
        $list = M('News')->where($map)->order("{$orderField} {$orderDirection}")->page("{$pageCurrent},{$pageSize}")->select();
        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['add_time'] = !empty($item['add_time']) ? date('Y-m-d H:i:s', intval($item['add_time'])) : '';
                $item['upd_time'] = !empty($item['upd_time']) ? date('Y-m-d H:i:s', intval($item['upd_time'])) : '';
            }
            $data['total'] = $total;
            $data['pages'] = ceil($total / $pageSize);
            $data['list'] = $list;
            apiReturn('获取数据', 200, $data);
        } else {
            apiReturn('无数据', 300);
        }
    }

    /**
     * 获取新闻详情
     * @author Pirlo.21 <375057009@qq.com>
     * @dateTime 2016-12-27
     */
    public function newsInfo() {
        $news_id = I('post.newsid', 0);
        if (empty($news_id)) {
            apiReturn('系统错误，无新闻', 300);
        }
        $rs = M('News')->find($news_id);
        if ($rs) {
            apiReturn('新闻详情', 200, $rs);
        } else {
            apiReturn('无数据', 300);
        }
    }

    /**
     * 获取新闻详情
     * @author Pirlo.21 <375057009@qq.com>
     * @dateTime 2016-12-27
     */
    public function newsDetail() {
        $news_id = I('get.newsid', 0);
        if (empty($news_id)) {
            apiReturn('系统错误，无新闻', 300);
        }
        $rs = M('News')->find($news_id);
        if ($rs) {
            $rs['add_time'] = empty($rs['add_time']) ? date("m-d H:i") : date("m-d H:i", $rs['add_time']);
            $rs['content'] = empty($rs['content']) ? "暂无内容" : html_entity_decode($rs['content']);
            $this->assign('rs', $rs);
            $this->display();
        } else {
            apiReturn('无数据', 300);
        }
    }

}
